<?php
error_reporting(E_ALL ^ E_NOTICE); // Report all errors except E_NOTICE warnings
//error_reporting(E_ALL); // Report all errors and warnings (very strict, use for testing only)
ini_set('display_errors', 1); // turn error reporting on
//ini_set('log_errors', 1); // log errors
//ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); // where to log errors
/*
IP To Country PHP Script by Mike Challis
Free PHP Scripts - www.642weather.com/weather/scripts.php
Download         - www.642weather.com/weather/scripts/ip-to-country.zip
Live Demo        - www.642weather.com/weather/ip-to-country.php
Contact Mike     - www.642weather.com/weather/contact_us.php

Version: 1.50 - 06-Dec-2008 see changelog.txt for changes

You are free to use and modify the code

This php code provided "as is", and Long Beach Weather (Michael Challis)
disclaims any and all warranties, whether express or implied, including
(without limitation) any implied warranties of merchantability or
fitness for a particular purpose.

Copyright (C) 2008 Mike Challis  (www.642weather.com/weather/contact_us.php)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

// this is a sample IP address for look up
// when used in your application, change this variable to your user's IP
// such as $ip = $_SERVER['REMOTE_ADDR'];
$ip = '70.56.98.195';

// Please change this URL to your own installation web site
// if I start getting massive hits, I will ban people.
// using it for a few demo test is fine though
// also this demo url is subject to change or be used for beta testing new code
$url = "http://www.642weather.com/demo/ip-to-country.php?action=check&ip=$ip&output=xml";

/*
expects data in this format as output from your ip-to-country.php:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<ip_data IP="70.56.98.195" IP_HOST="70-56-98-195.slkc.qwest.net" COUNTRY_NAME="UNITED STATES" COUNTRY_CODE2="US" ERROR="none" />
</response>


incorrect ip address parameter will output data in this format as output from your ip-to-country.php:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<ip_data IP="n/a" IP_HOST="n/a" COUNTRY_NAME="n/a" COUNTRY_CODE2="n/a" ERROR="Invalid IP address" />
</response>

*/

// Load XML info from remote site
$xml = simplexml_load_file($url);

// print_r($xml);

// convert simplexml to an array
foreach( $xml->ip_data[0]->attributes() as $key => $value ) {
   $ip_data[strtolower($key)] = (string)$value;
}

// print_r($ip_data);

// the data should now be in the $ip_data array
if ($ip_data['error'] == 'none') {
  /*
  The IP data should be good, use it anyway you want, you are a programmer right?
  The array contains:
  $ip_data['ip']
  $ip_data['ip_host']
  $ip_data['country_name']
  $ip_data['country_code2']
  */

  echo '
  IP = '.$ip_data['ip'].' <br>
  IP_HOST = '.$ip_data['ip_host'].' <br>
  COUNTRY_NAME = '.$ip_data['country_name'].' <br>
  COUNTRY_CODE2 = '.$ip_data['country_code2'].' <br>
  ERROR = '.$ip_data['error'];

  // how to block countries:
  // make a whitelist array of your allowed countries 2 letter country codes in CAPS
  // select whatever countries you want
  $whitelist = array('US', 'CA', 'GB', 'AU', 'DE');

  $block = 1;
  foreach ($whitelist as $v) {
       if($v == $ip_data['country_code2']) {
         $block = 0;
         break;
       }
  }

 // check if this country is blocked
 if ($block) {
    // do whatever you want when a country is blocked:
    // If this is e-commerce, I prefer a more vague error like:
    // Process Error 102, contact customer service
    echo '<p>Sample block message demonstrating the ability to block by country:
    This country is blocked, contact customer service</p>';
    exit;

 }

} else {

  // the IP data had an error
  // do something else
   echo 'IP lookup data ERROR = '.$ip_data['error'];
}

?>